<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  * {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
  }
  .body_init {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .step_content {
    display: flex;
    align-items: center;
    width: 400px;
    justify-content: space-between;
    position: relative;
  }
  .step_content::before{
    position: absolute;
    content: '';
    width: 100%;
    top: 50%;
    left: 0;
    height: 4px;
    background-color: #e0e0e0;
    z-index: -1;
  }
  .progress {
    position: absolute;
    top: 50%;
    left: 0;
    z-index: -1;
    height: 4px;
    width: 0;
    background-color: #3498db;
    transition: all 0.8s ease;

  }
  .step {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-color: #fff;
    border: 3px solid #e0e0e0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.8s ease;

  }
  .active {
    border-color: #3498db;
  }
  .btn-content {
    margin-top: 20px;
    display: flex;
    align-items: center;
    gap:20px;
    justify-content: center;
    
  }

  .btn {
    background-color: #3498db;
    padding: 8px 10px;
    color: #fff;
    border: 0;
    border-radius: 8px;
    cursor: pointer;
  }
  .btn:active {
    transform: scale(0.98);
  }

  .btn:disabled {
    background-color: #e0e0e0;
    cursor: not-allowed;
  }


</style>
<body class="body_init">
  <main>
    <div class="step_content">
      <div id="progress" class="progress"></div>
      <div id="step" class="step active">1</div>
      <div id="step" class="step">2</div>
      <div id="step" class="step">3</div>
      <div id="step" class="step">4</div>
    </div>
    <div class="btn-content">
      <button disabled id="back" class="btn">上一步</button>
      <button id="next" class="btn">下一步</button>
    </div>
  </main>

  <script>
    
    const useSelectIds = (ids) => {
      const values = {}
      ids.forEach(item => {
        if(document.querySelectorAll(`#${item}`).length == 1) {
          values[item] = document.querySelector(`#${item}`)
        } else {
          values[item] = document.querySelectorAll(`#${item}`)
        }
      })
      return values
    }

  const {progress,step:steps,back,next,} = useSelectIds(['progress','step','back','next'])

    // const selectId = (id) => {
    //   return document.querySelector(`${id}`)
    // }
    // const progress = selectId('#progress')
    // const steps = document.querySelectorAll('#step')
    // const back = selectId('#back')
    // const next = selectId('#next')

    let flag = 1

    const update = () => {
      steps.forEach((item,idx) => {
      if(flag > idx) {
        item.classList.add('active')
      } else {
        item.classList.remove('active')
      }
     })

     const actives = document.querySelectorAll('.active')
     progress.style.width = (actives.length -1) / (steps.length - 1) * 100 + '%'

     if(steps.length == actives.length) {
      next.disabled = true
     } else if (actives.length == 1) {
      back.disabled = true
     } else {
      next.disabled = false
      back.disabled = false
     }
    }


    const backEvent = () => {
      flag--
      update()
    }

    const nextEvent = () => {
      flag++
      update()
    }

    back.addEventListener('click',backEvent)
    next.addEventListener('click',nextEvent)
  
  </script>
</body>
</html>